package cn.iocoder.yudao.module.scrm.service.customer.customerrepository;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scrm.controller.admin.customer.vo.CustomerAutoUnlockVO;
import cn.iocoder.yudao.module.scrm.controller.admin.customer.vo.customerrepository.CustomerRepositoryRulePageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.customer.vo.customerrepository.CustomerRepositoryRuleSaveReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.customer.vo.customerrepository.DeptUseScopeByTypeReqVo;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.customerrepository.CustomerRepositoryRuleDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.customer.customerrepository.CustomerRepositoryRuleMapper;
import cn.iocoder.yudao.module.scrm.service.customer.CustomerService;
import com.fasterxml.jackson.core.JsonProcessingException;
import groovy.lang.Lazy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.CUSTOMER_REPOSITORY_RULE_NOT_EXISTS;


/**
 * 客户库规则 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class CustomerRepositoryRuleServiceImpl implements CustomerRepositoryRuleService {

    @Resource
    private CustomerRepositoryRuleMapper customerRepositoryRuleMapper;

    @Autowired
    @Lazy//延迟加载第一次使用时在加载
    private CustomerService customerService;

    @Resource
    @Lazy//延迟加载第一次使用时在加载
    private CustomerRepositoryService customerRepositoryService;

    @Override
    public Integer createCustomerRepositoryRule(CustomerRepositoryRuleSaveReqVO createReqVO) throws JsonProcessingException {
        //校验部门权限
        customerRepositoryService.deptUseScopeByType
                (new DeptUseScopeByTypeReqVo().setRepositoryId(createReqVO.getCustomerRepositoryId())
                        .setType(1).setOperationType(0));

        //自动解锁符合条件的客户锁定
        customerService.autoUnlockCustomer(CustomerAutoUnlockVO.builder()
                .build().setNewDeptIds(createReqVO.getDeptIds()).setType(1).setOperationType(0)
        );

        // 插入
        CustomerRepositoryRuleDO customerRepositoryRule = BeanUtils.toBean(createReqVO, CustomerRepositoryRuleDO.class);
        customerRepositoryRuleMapper.insert(customerRepositoryRule);
        // 返回
        return customerRepositoryRule.getId();
    }

    @Override
    public void updateCustomerRepositoryRule(CustomerRepositoryRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerRepositoryRuleExists(updateReqVO.getId());
        //自动解锁符合条件的客户锁定
        customerService.autoUnlockCustomer(CustomerAutoUnlockVO.builder()
                .build().setNewDeptIds(updateReqVO.getDeptIds()).setRepositoryRuleId(updateReqVO.getId())
                .setNewCustomerCountLock(updateReqVO.getCustomerCountLock()).setType(1).setOperationType(2)
        );
        // 更新
        CustomerRepositoryRuleDO updateObj = BeanUtils.toBean(updateReqVO, CustomerRepositoryRuleDO.class);
        customerRepositoryRuleMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerRepositoryRule(Integer id) {
        // 校验存在
        validateCustomerRepositoryRuleExists(id);
        //自动解锁符合条件的客户锁定
        /*customerService.autoUnlockCustomer(CustomerAutoUnlockVO.builder()
                .build().setRepositoryRuleId(id).setType(1).setOperationType(1)
        );*/
        // 删除
        customerRepositoryRuleMapper.deleteById(id);
    }

    private void validateCustomerRepositoryRuleExists(Integer id) {
        if (customerRepositoryRuleMapper.selectById(id) == null) {
            throw exception(CUSTOMER_REPOSITORY_RULE_NOT_EXISTS);
        }
    }

    @Override
    public CustomerRepositoryRuleDO getCustomerRepositoryRule(Integer id) {
        return customerRepositoryRuleMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerRepositoryRuleDO> getCustomerRepositoryRulePage(CustomerRepositoryRulePageReqVO pageReqVO) {
        return customerRepositoryRuleMapper.selectPage(pageReqVO);
    }

}